package com.cbdp.admin.controller;

import com.cbdp.admin.bean.UserBean;
import com.cbdp.admin.view.UserView;
import com.cbdp.common.exception.BizException;
import com.cbdp.common.util.BeanUtil;
import com.cbdp.dao.entity.UserEntity;
import com.cbdp.dao.entity.UserEntityExample;
import com.cbdp.dao.mapper.UserEntityMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * com.cbdp.admin.controller
 *
 * @author 白雨浓
 * date: 2018/10/26 11:33
 * content:
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

    private final UserEntityMapper userEntityMapper;

    @GetMapping
    public Object test(Integer k) {
        if (k < 0) {
            throw new BizException("error error");
        }

        UserEntityExample example = new UserEntityExample();

        var list = userEntityMapper.selectByExample(example);

        return BeanUtil.to(list, UserBean.class);
    }

    @PostMapping
    public Object postTest(@RequestBody UserView userView) {

        UserEntity entity = BeanUtil.to(userView, UserEntity.class);

        entity.setState(true);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());

        userEntityMapper.insert(entity);
        return true;
    }

}
